package android.money.db;

import java.io.FileNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.money.data.Account;
import android.util.Log;

public class AccountManager {

	private SQLiteDatabase mDb;

	private Context ctx;

	   private static class DatabaseHelper extends SQLiteOpenHelper {

	        DatabaseHelper(Context context) {
	            super(context, DBInfo.DATABASE_NAME, null, DBInfo.DATABASE_VERSION);
	        }

	        @Override
	        public void onCreate(SQLiteDatabase db) {
	        	db.execSQL(DBInfo.TABLE_TRANSACTION_CREATE);
	        	db.execSQL(DBInfo.TABLE_ACCOUNT_CREATE);
	        }

	        @Override
	        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	            Log.w("AccountManager", "Upgrading database from version " + oldVersion + " to "
	                    + newVersion + ", which will destroy all old data");
	            db.execSQL("DROP TABLE IF EXISTS "+DBInfo.TABLE_ACCOUNT_NAME);
	            db.execSQL("DROP TABLE IF EXISTS "+DBInfo.TABLE_TRANSACTION_NAME);
	            onCreate(db);
	        }
	    }	
	public AccountManager(Context ctx) {
		this.ctx = ctx;
	}

	public AccountManager open() {
		mDb = new DatabaseHelper(ctx).getWritableDatabase();
		return this;
	}

	public void close() {
		mDb.close();
	}

	public boolean addAccount(Account account) {
		ContentValues initialValues = new ContentValues();

		initialValues.put(DBInfo.account_number, account.getNumber());
		initialValues.put(DBInfo.account_sec_code, account.getSec_code());
		initialValues.put(DBInfo.account_type, account.getType().getType());
		initialValues.put(DBInfo.account_expire_month, account
				.getExpire_month());
		initialValues.put(DBInfo.account_expire_year, account.getExpire_year());
		initialValues.put(DBInfo.account_holder_name, account.getHolderName());
		initialValues.put(DBInfo.account_name, account.getName());
		initialValues.put(DBInfo.account_piority, account.getPiority());

		if (mDb.insert(DBInfo.TABLE_ACCOUNT_NAME, null, initialValues) < 0) {
			return false;
		}
		return true;

	}

	public Cursor fetchAllAccounts() {
		return mDb.query(DBInfo.TABLE_ACCOUNT_NAME, new String[] {
				DBInfo.account_id, DBInfo.account_name, DBInfo.account_number,
				DBInfo.account_expire_month, DBInfo.account_expire_year,
				DBInfo.account_sec_code, DBInfo.account_holder_name,
				DBInfo.account_type }, null, null, null, null, null);
	}

	public Cursor fetchAccount(int id) {
		Cursor result = mDb.query(DBInfo.TABLE_ACCOUNT_NAME, new String[] {
				DBInfo.account_id, DBInfo.account_name,
				DBInfo.account_holder_name, DBInfo.account_number,
				DBInfo.account_sec_code, DBInfo.account_type,
				DBInfo.account_expire_month, DBInfo.account_expire_year,
				DBInfo.account_piority }, DBInfo.account_id + " = " + id, null,
				null, null, null);
		if (result!=null){
			result.moveToFirst();
		}
		return result;
	}

	public boolean deleteAccount(int id) {
		return mDb.delete(DBInfo.TABLE_ACCOUNT_NAME, DBInfo.account_id + " = "
				+ id, null) > 0;
	}

	public boolean updateAccount(int id, Account account) {
		ContentValues args = new ContentValues();
		args.put(DBInfo.account_number, account.getNumber());
		args.put(DBInfo.account_sec_code, account.getSec_code());
		args.put(DBInfo.account_type, account.getType().getType());
		args.put(DBInfo.account_expire_month, account.getExpire_month());
		args.put(DBInfo.account_expire_year, account.getExpire_year());
		args.put(DBInfo.account_holder_name, account.getHolderName());
		args.put(DBInfo.account_name, account.getName());
		args.put(DBInfo.account_piority, account.getPiority());
		return mDb.update(DBInfo.TABLE_ACCOUNT_NAME, args, DBInfo.account_id
				+ " = " + id, null) > 0;
	}
}
